#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author  : Lee
# @File    : format_test_data.py
# @Time    : 2024/1/5 15:31
import os
import csv
from cyber_record.record import Record


def get_origin_debug_data(file_path):
    _format_data_list = list()
    with open(file_path, 'r', encoding='utf-8') as f:
        for _line in f.readlines():
            _line_data = _line.rstrip().split(" ")
            _format_data_list.append([_line_data[-1], _line_data[-9], _line_data[-7], _line_data[-19], _line_data[-15]])

    _file_name = os.path.basename(file_path)
    # 打开或创建 CSV 文件并指定写入模式
    with open(os.path.join("data", f"_{_file_name}"), mode='w',  newline="") as file:
        # 初始化 CSV writer 对象
        writer = csv.writer(file)

        # 写入每一行数据
        for row in _format_data_list:
            # print(row)
            writer.writerow(row)


def get_csv_from_record(file_path):
    for record_name in os.listdir(file_path):
        if ".record" in record_name:
            _record = Record(os.path.join(file_path, record_name))
            for channel_name, data, timestamp in _record.read_messages():
                if "/App/JobRequest" in channel_name:
                    print(data)
            #     print(record_name, data)


if __name__ == '__main__':
    # get_origin_debug_data("data/debug_2024-1-5-10-19-56.csv")
    # get_origin_debug_data("data/debug_2024-1-5-10-30-25.csv")
    # get_origin_debug_data("data/debug_2024-1-5-10-40-36.csv")
    # get_origin_debug_data("data/debug_2024-1-5-10-51-30.csv")
    get_csv_from_record("record_files")
